//
//  GYProductCategoryVC.m
//  GYDemo
//
//  Created by mac on 2018/3/22.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYProductCategoryVC.h"
#import "Head.h"
#import "GYProductCollectionView.h"
#import "GYProductCateModel.h"
#import "GYSearchVC.h"
#import "GYCategoryListVC.h"
#import "GYNewShopListVC.h"
@interface GYProductCategoryVC ()
@property(nonatomic,assign)NSInteger currentFirstIndex;
@property(nonatomic,strong)GYProductCollectionView* collectionView;
@property(nonatomic,strong)GYProductCateModel* productModel;
@end

@implementation GYProductCategoryVC
#pragma mark - getter and setter
-(GYProductCollectionView *)collectionView
{
    if (!_collectionView) {
        @WeakSelf;
        _collectionView=[[GYProductCollectionView alloc] initWithFrame:CGRectMake(WFCGFloatX(110), 0, WFCGFloatX(375-130),WF_SCREEN_HEIGHT-self.topShelterHeight-self.bottomShelterHeight)];
        _collectionView.cellBack = ^(NSString *pid) {
            [selfp pushListVC:pid];
        };
    
    }
    return _collectionView;
}
-(GYProductCateModel *)productModel
{
    if (!_productModel) {
        _productModel=[GYProductCateModel shareInterface];
    }
    return _productModel;
}
#pragma mark - event
-(void)toSearch:(id)sender
{
    GYSearchVC* gy=[[GYSearchVC alloc] init];
    [self.navigationController pushViewController:gy animated:YES];
}
#pragma mark - method

-(void)initialization
{
    [self setNavgationTheme:1];
    //导航和背景
    [self loadTitleV];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view setBackgroundColor:RGBACOLOR(240, 240, 240, 1)];
    
   // [self.navigationItem setRightBarButtonItem: [self barButtonItemWithTitle:@"搜索" andAction:@selector(toSearch:)]];
    //table
    [self.baseTable setBackgroundColor:[UIColor whiteColor]];
    self.baseTable.separatorColor=RGBACOLOR(233, 233, 233, 1);
    self.baseTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.baseTable.showsVerticalScrollIndicator = NO;
    self.baseTable.width=WFCGFloatX(90);
    [self.view addSubview:self.baseTable];
    [self.view addSubview:self.collectionView];
    [self.productModel requestCategory];

}
-(void)reloadView
{
    [self.baseTable reloadData];
    [self.collectionView refreshData:self.productModel.dataSource[_currentFirstIndex]];
}
-(void)viewModelBand
{
    GCBaseObservationModel *observation1 = [GCBaseObservationModel new];
    observation1.observation = self.productModel;
    observation1.keyPath = @"secLevelArr";
    observation1.handler = ^(NSString *keyPath) {
        [self reloadView];
    };
    [self registObservation:observation1];
}
///标题
-(void)loadTitleV
{
    UIView *titleV = [[UIView alloc]init];
    [titleV setFrame:CGRectMake(0, 0, WF_UI_VIEW_WIDTH, 44)];
    [titleV setBackgroundColor:[UIColor clearColor]];
    

    
    UIImageView *searchv = [[UIImageView alloc]init];
    [searchv addTarget:self action:@selector(toSearch:)];
    [titleV addSubview:searchv];
    [searchv setFrame:CGRectMake(10, 7, WF_UI_VIEW_WIDTH - 20, 30)];
    [searchv setBackgroundColor:Main_Color_BG];
    [WFFunctions WFUIaddbordertoView:searchv radius:15 width:0.0f color:nil];
    
    UIImageView *findVI = [[UIImageView alloc]init];
    [findVI setImage:[UIImage imageNamed:@"home_search"]];
    [searchv addSubview:findVI];
    [findVI setFrame:CGRectMake(10, (30-findVI.image.size.height)/2, findVI.image.size.width, findVI.image.size.height)];

    
    
    
    UIButton* btnrr = [[UIButton alloc]init];
    btnrr.frame=CGRectMake(30, 0, 150, 30);
    [btnrr setTitle:@"请输入关键字搜索" forState:UIControlStateNormal];
    [btnrr setTitleColor:HEXCOLOR(@"#999999") forState:UIControlStateNormal];
    btnrr.titleLabel.font=SYSTEMFONT(15);
    [searchv addSubview:btnrr];
    
    @WeakSelf;
    [btnrr handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        
        GYSearchVC* gy=[[GYSearchVC alloc] init];
        [selfp.navigationController pushViewController:gy animated:YES];
    }];
    
    [self.navigationController.navigationBar addSubview:titleV];
}

-(void)pushListVC:(NSString* )pid
{
    GYNewShopListVC* gy=[[GYNewShopListVC alloc] init];
    gy.passID=pid;
    [self.navigationController pushViewController:gy animated:YES];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialization];
    [self viewModelBand];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - delegate
#pragma mark - table delegate & data source
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"00reply";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.separatorInset = UIEdgeInsetsMake(0, 57, 0, 0);
        cell.backgroundColor = RGBACOLOR(240, 240, 240, 1);
        
        //名字
        UILabel *labName = [[UILabel alloc]init];
        labName.frame = WFCGRectMake(5, 0, 80, 46);
        labName.backgroundColor = [UIColor clearColor];
        labName.textColor = Main_Color_Black;
        labName.adjustsFontSizeToFitWidth = YES;
        labName.textAlignment = NSTextAlignmentCenter;
        labName.font = [UIFont systemFontOfSize:13 withScale:autoSizeScaleY];
        labName.tag = 12;
        [cell.contentView addSubview:labName];
        
        UIImageView* locationIV=[[UIImageView alloc] init];
        locationIV.x=0;
        locationIV.y=WFCGFloatY(33/2);
        locationIV.width=3;
        locationIV.tag=13;
        locationIV.height=WFCGFloatY(13);
        [cell.contentView addSubview:locationIV];
        locationIV.backgroundColor=Main_Color_Red;
        
    }
    
    UILabel *labName = (UILabel*)[cell viewWithTag:12];
    UIImageView *IV = (UIImageView*)[cell viewWithTag:13];
    
    GYProductCateModel* model= self.productModel.dataSource[indexPath.row];
    labName.text =model.titleStr;

    if (_currentFirstIndex != indexPath.row) {
        [labName setTextColor:Main_Color_Black];
        [IV setHidden:YES];
        [cell.contentView setBackgroundColor:[UIColor whiteColor]];
    }else{
        [labName setTextColor:Main_Color_Red];
        [IV setHidden:NO];
        [cell.contentView setBackgroundColor:RGBACOLOR(243, 245, 247, 1)];
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.productModel.dataSource count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WFCGFloatY(46);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_currentFirstIndex != indexPath.row) {
        [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        [tableView reloadData];
        _currentFirstIndex = indexPath.row;

        GYProductCateModel* model=self.productModel.dataSource[indexPath.row];
        [self.productModel setSecLevelArr:model.secLevelArr];
    }
}
#pragma mark - 服务器交互
#pragma mark 评价类别

@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
